#include "ScnsSmctp.c.h"

#if defined(SCNS_SMCTP_ENABLE)&&SCNS_SMCTP_ENABLE==1

#include "ScnsMenu.h"
#include "ScnsMath.h"
#include "ScnsMath.h"

#include "main.h"
#include "usart.h"
#include "ScnsBeep.h"
#include "ScnsLed.h"
#include "rtc.h"
#include <time.h>

extern DMA_HandleTypeDef hdma_lpuart1_tx;

const ScnsSmctpChanelConfig scnsSmctpChanelConfig[SCNS_SMCTP_CHANNEL_MAX]={
    [SCNS_SMCTP_CHANNEL_MENU]={.callback=scnsMenuReceiveInterrupt},
};

ScnsStatus scnsBspSmctpCanSend(void)
{
    return SCNS_STATUS_OK;
}

void scnsBspSmctpSend(const uint8*buf,uint32 *len)
{
    HAL_UART_Transmit_DMA(&huart1,(uint8*)buf,*len);
}

void scnsBspSmctpReceiveIdleDmaStart(uint8*buf,uint32 len)
{
    HAL_UARTEx_ReceiveToIdle_DMA(&huart1,buf,len);
}

void scnsBspSmctpReceiveIdleDmaStop(uint8*buf,uint32 len)
{
    HAL_UART_AbortReceive(&huart1);
}

int32 scnsBspSmctpReceiveIdleDmaGetLength(void)
{
    return 0;
}

void scnsBspSmctpReceiveAbsoluteTime(uint64 us)
{
    time_t lt=us/1000000;
    struct tm*ptr=localtime(&lt);
    RTC_TimeTypeDef rtcTime={.Hours=ptr->tm_hour,.Minutes=ptr->tm_min,.Seconds=ptr->tm_sec};
    RTC_DateTypeDef rtcDate={.Date=ptr->tm_mday,.Month=ptr->tm_mon+1,.Year=(ptr->tm_year+1900)%100};
    //HAL_RTC_SetTime(&hrtc,&rtcTime,RTC_FORMAT_BIN);
    //HAL_RTC_SetDate(&hrtc,&rtcDate,RTC_FORMAT_BIN);
    //hxPrintfFL("second:%d\n",ptr->tm_sec);
    //hxPrintfFL("minute:%d\n",ptr->tm_min);
    //hxPrintfFL("hour:%d\n",ptr->tm_hour);
    //hxPrintfFL("mday:%d\n",ptr->tm_mday);
    //hxPrintfFL("month:%d\n",ptr->tm_mon+1);
    //hxPrintfFL("year:%d\n",ptr->tm_year+1900);
    (void)rtcTime;
    (void)rtcDate;
}

#endif
